package juc;

import java.util.concurrent.CountDownLatch;

public class LatchDemo implements Runnable
{
    private CountDownLatch countDownLatch;

    public LatchDemo(CountDownLatch countDownLatch)
    {
        this.countDownLatch = countDownLatch;
    }


    static int i = 10;
    public static void main(String[] args) throws InterruptedException
    {
        //这次彻底的将countDownLatch给弄明白了
        final CountDownLatch countDownLatch = new CountDownLatch(5);
        LatchDemo latchDemo = new LatchDemo(countDownLatch);
        for (int j = 0; j < 5; j++)
        {
            //多线程启动
            new Thread(latchDemo,String.valueOf(j)).start();
        }
        countDownLatch.await();
        System.out.println("这是主线程");
    }



    @Override
    public void run()
    {
        synchronized (LatchDemo.class)
        {
            try
            {
                System.out.println(Thread.currentThread().getName()+"这是："+i);
                i--;
            }finally
            {
                countDownLatch.countDown();
            }
        }
    }
}
